<template>
  <div>
    <BreakSpace label="基本信息" :containerStyle="{ marginBottom: '15px' }" />
    <FormPanel
      ref="formPanel"
      :list="formList"
      labelWidth="130"
      :cols="2"
      :formType="type"
      :isSubmitBtn="false"
      @formChange="formChangeHandle"
    />
    <div
      :style="{
        textAlign: 'center'
      }"
    >
      <el-button @click="saveDialog" :loading="saveLoading" type="primary">保 存</el-button>
      <el-button @click="closeDialog">返 回</el-button>
    </div>
  </div>
</template>

<script>
  import { dictionary } from '@/utils/dictMixin';
  import {addCentralStorehouse,updateCentralStorehouse,selectByDM} from '@/api/partsmanage/partWarehouseManage/centralStorehouse/index';

  export default {
    name: "updateCentral",
    mixins: [dictionary],
    props: {
      type: {
        type: String,
        default: 'add',
      },
      rowData: {
        type: Object,
        default: () => ({})
      }
    },
    data() {
      this.saveData = null;
      return {
        saveLoading: false,
        formList: this.createFormList(),
        fetchApi: null,
        fetchParams: null,
      };
    },
    created() {
      this.init();
    },
    methods: {
      selectChange(val) {
        console.log('val', val);
        this.rowSelectData = val;
      },
      init() {
        console.info(this.type)
        console.info(this.rowData)
        if (this.type == 'edit'){
          this.formList.find(item=>item.fieldName=="bsckdm").disabled=true
          this.formList.forEach(x => {
            x.initialValue = this.rowData[x.fieldName];
          });
        }
      },
      createFormList() {
        return [
          {
            type: 'INPUT',
            label: '仓库代码',
            fieldName: 'bsckdm',
            rules: [
              { required: true, message: '请输入仓库代码', trigger: 'blur' },
              { pattern: /^[a-zA-Z0-9]+$/, message: '请输入英文字母或数字', trigger: 'blur' }
            ]
          },
          {
            type: 'INPUT',
            label: '仓库名称',
            fieldName: 'bsckmc',
            maxlength: 50,
            rules: [{ required: true, message: '请输入仓库名称', trigger: 'blur' }]
          },
          {
            type: 'SELECT',
            label: '状态',
            fieldName: 'xtqybz',
            itemList:this.createDictList('1003'),
            initialValue: 10031001,
          },
          {
            type: 'TEXT_AREA',
            label: '备注',
            fieldName: 'bsbzhu',
            maxlength: 300
          }
        ]
      },
      formChangeHandle(val) {
        this.saveData = val;
      },
      async saveDialog() {
        console.info(this.saveData)
        console.info(this.rowData)
        console.info('11111111111111111111')
        const err = this.$refs.formPanel.SUBMIT_FORM();
        if (err === null) {
          return false
        }
        this.saveLoading = true
        if (this.type === 'edit') {
          this.saveData.id = this.rowData.id
          const res = await updateCentralStorehouse(this.saveData);
          if (res.resultCode === 200 ) {
            this.closeDialog('refresh');
          }
        }else{
          const result = await selectByDM(this.saveData)
          console.info(result)
          if (result.data == null){
            const res = await addCentralStorehouse(this.saveData);
            if (res.resultCode === 200 ) {
              this.closeDialog('refresh');
            }
          }else{
            if (result.data.xtqybz == 10031002){
              this.$notify.error({title: '提示信息', message: '此仓库代码已存在！但已被禁用，如需启用请前往启用！'});
            }else{
              this.$notify.error({title: '提示信息', message: '此仓库代码已存在！'});
            }
          }

        }
        this.saveLoading = false
      },
      closeDialog(param) {
        this.$emit('close', {visible: false, type: param});
      }
    }
  }
</script>

<style scoped>

</style>
